package bbs.net;
import bbs.model.CommitBack;
import model.OwnUser;
import utils.TimeUtil;

import com.alibaba.fastjson.JSON;
import com.android.volley.*;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.orm.SugarRecord;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发送回复
 * Created by nimi on 2015/11/17.
 */
public class SendCommitBack {
    Upload upload;
    Download download;
    Delegate delegate;
    public  SendCommitBack(Upload upload){
        this.upload=upload;
    }

    ReplayBoard replayBoard;


    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public Delegate getDelegate() {
        return delegate;
    }

    public Download getDownload() {
        return download;
    }

    public void setDownload(Download download) {
        this.download = download;
    }

    OwnUser ownUser;
    {
        List<OwnUser> ownUsers = SugarRecord.listAll(OwnUser.class);
        if (!ownUsers.isEmpty()) {
            ownUser = ownUsers.get(0);
        }
    }


    public void get() {
        replayBoard = new ReplayBoard();
        replayBoard.setUser_ID(ownUser.getUser_ID());
        replayBoard.setBoard_ID(upload.getCommitBack().getSubmit().getBoard_ID());
        replayBoard.setFeedback_Content(upload.getCommitBack().getContentBack());
        replayBoard.setFeedback_Time(TimeUtil.timestamp2Format(System.currentTimeMillis()));

        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/BBS/ReplyBoard";
        RequestQueue requestQueue = VolleySingleton.getVolleySingleton(MyApplication.share()).getRequestQueue();
        StringRequest stringRequest =new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            public void onResponse(String response) {
                afterGet(response);
                if(delegate!=null)
                    delegate.onResponse(response,SendCommitBack.this);
            }}, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error, SendCommitBack.this);

            }
        })

        {
            @Override
            protected Map<String, String> getParams () {
                Map map = new HashMap();
                map.put("data",JSON.toJSONString(replayBoard));
              /*  map.put("submitContent",upload.getSubmit().getBoard_Content());
                map.put("subjectId",upload.getSubjectId());
                map.put("userId",upload.getSubmit().getUser().getTopic_ID());
                */
                return map;
            }
            @Override
            protected Response<String> parseNetworkResponse (NetworkResponse response){
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }

        };
        requestQueue.add(stringRequest);
    }

    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
    }
    public SendCommitBack() {
        super();
    }
    // 上行数据
    public static class Upload {
        // 回复
        CommitBack commitBack;

        public CommitBack getCommitBack() {
            return commitBack;
        }

        public void setCommitBack(CommitBack commitBack) {
            this.commitBack = commitBack;
        }

    }

    // 下行数据
    public static class Download {
        // 状态
        int status;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

    }
    public static interface Delegate{
        public void onResponse(String response, SendCommitBack sendCommitBack);
        public void onErrorResponse(VolleyError error, SendCommitBack sendCommitBack);
    }


    private class ReplayBoard {
        private String user_ID;
        private String feedback_Time;
        private String feedback_Content;
        private String board_ID;

        public String getUser_ID() {
            return user_ID;
        }
        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }
        public String getFeedback_Time() {
            return feedback_Time;
        }
        public void setFeedback_Time(String feedback_Time) {
            this.feedback_Time = feedback_Time;
        }
        public String getFeedback_Content() {
            return feedback_Content;
        }
        public void setFeedback_Content(String feedback_Content) {
            this.feedback_Content = feedback_Content;
        }
        public String getBoard_ID() {
            return board_ID;
        }
        public void setBoard_ID(String board_ID) {
            this.board_ID = board_ID;
        }
    }
}
